package learn_0329;

import java.util.HashSet;

public class Dfs_learn_形状不一样 {
    int[][] dir = new int[][]{{-1,0},{1,0},{0,-1},{0,1}};

    static int ilandNum(char[][] grid){
        int c = grid.length;
        int k = grid[0].length;
        HashSet<String> island =  new HashSet<>();
        for(int i =0;i<c;i++){
            for (int j =0;j<k;j++){
                if(grid[i][j] == '1'){
                    StringBuilder stringBuilder = new StringBuilder();
                    dfs(grid,i,j,stringBuilder,0);
                    island.add(stringBuilder.toString());
                }
            }
        }
        return island.size();
    }

    static void dfs(char[][] grid,int i ,int j,StringBuilder stringBuilder,int dir){
        int c = grid.length;
        int k = grid[0].length;
        if(i<0 || j<0 || i>=c || j>=k){
            return;
        }
        if (grid[i][j] == '0'){
            return;
        }
//        if(visited[i][j] == true){
//            return;
//        }
        grid[i][j] = '0';
        stringBuilder.append(dir);
        stringBuilder.append(',');
        dfs(grid,i,j+1,stringBuilder,1);
        dfs(grid,i+1,j,stringBuilder,2);
        dfs(grid,i-1,j,stringBuilder,3);
        dfs(grid,i,j-1,stringBuilder,4);

        stringBuilder.append(-dir);
        stringBuilder.append(',');

    }
    public static void main(String[] args) {
//        char [][] grid = new char[][]{{'1','1','1','1','0'},
//                {'1','1','0','1','0'},
//                {'1','1','0','0','0'},
//                {'0','0','0','0','0'}
        char [][] grid = new char[][]{{'1','1',0,'1',1},
                {'1','0','0','0','0'},
                {'0','0','0','0','1'},
                {'1','1','0','1','1'}
};

        int result = ilandNum(grid);
        System.out.print(result);

    }
}
